﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Infrastructure.HitMonitor;
using VirtualPress.Portal.Biz;
using VirtualPress.Portal.WebLogic;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Portal.Web.Areas.Magazine.Controllers
{
#if !DEBUG
        [ErrorHandle]
#endif
    [Compress]
    public class HomeController : Controller
    {
        //
        // GET: /Magazine//
        public ActionResult Index(int page=0)
        {
            var magazine = iCache.Get(String.Format("MagazineBiz.GetAllMagazines_{0}_{1}OfItemListBox", page, 12), () =>
            {
                return new ItemListBox<Periodical>(MagazineBiz.GetAllMagazines(page, 12))
                    .BuildPage(MagazineBiz.GetAllMagazinesForpage(), page, 9, new PageParameter() { Style = "home" });
            }, 600);
            ViewBag.Magazine = magazine;
            return View();
        }

        public ActionResult Magazine(int id,int page =0)
        {
            var journal = new Periodical().Find(id.ToString());
            if (journal == null)
            {
                return RedirectToAction("Index");
            }
            ViewBag.Journal = journal;
            var magazineItem = iCache.Get(String.Format("MagazineBiz.GetAllPublishJournalItem_{0}_{1}_{2}_OfItemListBox", id, page, 12), () =>
            {
                return new ItemListBox<MagazineItem>(MagazineBiz.GetAllPublishJournalItem(id, page, 12))
                    .BuildPage(MagazineBiz.GetAllPublishJournalItemForPage(id), page, 12, new PageParameter() { Style = "home" });
            }, 600);
            ViewBag.MagazineItem = magazineItem;
            ViewBag.Hit = HitMonitor.Instance(HitName.Journal, id).GetValue<JournalHitPack>(true, (int)JournalGetAction.visit);
            return View();
        }

        public ActionResult Details(int id)
        {
            try
            {
                var magazine = new MagazineItem().Find(id.ToString());
                if (magazine == null) 
                {
                    return RedirectToAction("Magazine", new { id = id });
                }
                var journal = new Periodical().Find(magazine.PeriodicalId.ToString());
                if (journal == null)
                {
                    return RedirectToAction("Magazine", new { id = id });
                }
                string[] orgfiles = System.IO.Directory.GetFiles(
                    Server.MapPath(magazine.PagePath));
                List<string> file = new List<string>();
                foreach (var item in orgfiles)
                {
                    try
                    {
                        var fileInfo = new FileInfo(item);
                        file.Add(fileInfo.Name);
                    }
                    catch { }
                }
                file = file.OrderBy(c => c).Select(c => magazine.PagePath + c).ToList();
                ViewBag.Files = file;
                ViewBag.Magazine = magazine;
                ViewBag.Journal = journal;
                return View();
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.Journal, LogLevel.Error, e);
                return RedirectToAction("Magazine", new { id = id });
            }
        }

    }
}
